package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.DividendRecordVo;
import org.locker.domain.bo.DividendRecordBo;
import org.locker.service.IDividendRecordService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 管理员分润记录
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/dividendRecord")
public class DividendRecordController extends BaseController {

    private final IDividendRecordService dividendRecordService;

    /**
     * 查询管理员分润记录列表
     */
    @SaCheckPermission("device:dividendRecord:list")
    @GetMapping("/list")
    public TableDataInfo<DividendRecordVo> list(DividendRecordBo bo, PageQuery pageQuery) {
        return dividendRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出管理员分润记录列表
     */
    @SaCheckPermission("device:dividendRecord:export")
    @Log(title = "管理员分润记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DividendRecordBo bo, HttpServletResponse response) {
        List<DividendRecordVo> list = dividendRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "管理员分润记录", DividendRecordVo.class, response);
    }

    /**
     * 获取管理员分润记录详细信息
     *
     * @param dividendId 主键
     */
    @SaCheckPermission("device:dividendRecord:query")
    @GetMapping("/{dividendId}")
    public R<DividendRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dividendId) {
        return R.ok(dividendRecordService.queryById(dividendId));
    }

    /**
     * 新增管理员分润记录
     */
    @SaCheckPermission("device:dividendRecord:add")
    @Log(title = "管理员分润记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DividendRecordBo bo) {
        return toAjax(dividendRecordService.insertByBo(bo));
    }

    /**
     * 修改管理员分润记录
     */
    @SaCheckPermission("device:dividendRecord:edit")
    @Log(title = "管理员分润记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DividendRecordBo bo) {
        return toAjax(dividendRecordService.updateByBo(bo));
    }

    /**
     * 删除管理员分润记录
     *
     * @param dividendIds 主键串
     */
    @SaCheckPermission("device:dividendRecord:remove")
    @Log(title = "管理员分润记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dividendIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dividendIds) {
        return toAjax(dividendRecordService.deleteWithValidByIds(List.of(dividendIds), true));
    }
}
